package cn.vinord.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;

@Configuration
@ConditionalOnClass(WxMaService.class)
public class WxMaConfiguration {

	@Autowired
	private Properties properties;

	@Bean
	@ConditionalOnMissingBean
	public WxMaConfig config() {
		WxMaInMemoryConfig config = new WxMaInMemoryConfig();
		config.setAppid(this.properties.getWxMaProperties().getAppid());
		config.setSecret(this.properties.getWxMaProperties().getSecret());
		config.setToken(this.properties.getWxMaProperties().getToken());
		config.setAesKey(this.properties.getWxMaProperties().getAesKey());
		config.setMsgDataFormat(this.properties.getWxMaProperties().getMsgDataFormat());
		return config;
	}

	@Bean
	@ConditionalOnMissingBean
	public WxMaService wxMaService(WxMaConfig config) {
		WxMaService service = new WxMaServiceImpl();
		service.setWxMaConfig(config);
		return service;
	}
}
